package cn.edu.zucc.online_market.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.online_market.itf.IProductManager;
import cn.edu.zucc.online_market.model.BeanProduct;
import cn.edu.zucc.online_market.model.BeanProductClass;
import cn.edu.zucc.online_market.util.BaseException;
import cn.edu.zucc.online_market.util.DBPool;
import cn.edu.zucc.online_market.util.DbException;

public class ProductManager implements IProductManager {

	
	public List<BeanProduct> product_query() {
		// TODO Auto-generated method stub
		List <BeanProduct> result = null;
		return result;
	}

	@Override
	public List<BeanProduct> loadProductsByProductClass(BeanProductClass product_class) throws BaseException {
		List<BeanProduct> product_list = new ArrayList<BeanProduct>();
		BeanProduct p = null;
		Connection conn = null;
		
		try {
			conn = DBPool.getInstance().getConnection();	
			java.sql.PreparedStatement pst;
			java.sql.ResultSet rs;
			String sql;
			sql = "select product_id,class_id,product_name,product_price,product_count from product_info where class_id = ?;";
			pst = conn.prepareStatement(sql);
			pst.setInt(1, product_class.getProduct_class_id());
			rs = pst.executeQuery();
			while(rs.next()) {
				p = new BeanProduct();
				p.setProduct_id(rs.getInt(1));
				p.setProduct_class_id(rs.getInt(2));
				p.setProduct_name(rs.getString(3));
				p.setPrice(rs.getFloat(4));
				p.setCount(rs.getFloat(5));
				product_list.add(p);
			}
			rs.close();
			pst.close();
		}catch(SQLException e) {
			e.printStackTrace();
			throw new DbException(e);
		}finally {
			if(!(conn==null)) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return product_list;
	}

}
